//# sourceURL=http://uMapApp/Module/UavFlight/UavFlightsModule
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../Road/StakeNumPanel"
    , "../StakeNum/Mileage"
    , "./UavFlight"
    , "../../UI/Layer"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel
    , StakeNumPanel
    , Mileage
    , UavFlight
    , Layer
) {

    function UavFlightsModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "航拍视频";
        _this.icon = _this.app.icons.UAV;

        _this.panels = [];
        _this.routes = {};
        _this.items = {};

        _this.layer = new Layer({
            "app": _this.app
        });

        //整体功能按钮
        _this.globeButtonPanel = new FloatPanel({"class": "globeButtonPanel InspectionModule"});
        _this.globeButtonPanel.label = new mhjs.Div({"class": "label"});
        _this.globeButtonPanel.label.text("航线");

        _this.globeButtonPanel.routeSelect = new mhjs.Select({"class": "select"});

        _this.globeButtonPanel
            .append(_this.globeButtonPanel.label)
            .append(_this.globeButtonPanel.routeSelect)
        ;

        _this.app.ui.layout.top.append(_this.globeButtonPanel);
        _this.panels.push(_this.globeButtonPanel);


        //整体功能按钮
        _this.playPanel = new FloatPanel({"class": "playPanel InspectionModule"});
        _this.playPanel.backwardButton = new mhjs.Div({"class": "backward button"});
        _this.playPanel.playButton = new mhjs.Div({"class": "play button"});
        _this.playPanel.forwardButton = new mhjs.Div({"class": "forward button"});

        _this.playPanel
            .append(_this.playPanel.backwardButton)
            .append(_this.playPanel.playButton)
            .append(_this.playPanel.forwardButton)
        ;

        _this.playPanel.playButton.on("click", function (button) {
            _this.togglePlay();
        });

        _this.playPanel.backwardButton.on("click", function (button) {

            _this.gotoPreviousFlight();
            if (_this.isPlaying) {
                _this.currentFlight.play();
            }


        });

        _this.playPanel.forwardButton.on("click", function (button) {
            _this.gotoNextFlight();
            if (_this.isPlaying) {
                _this.currentFlight.play();
            }
        });

        _this.app.ui.layout.bottom.append(_this.playPanel);
        _this.panels.push(_this.playPanel);

        //桩号导航
        _this.stakeNumPanel = new StakeNumPanel({
            "app"  : _this.app,
            "class": "InspectionModule"
        });

        _this.stakeNumPanel.on("stakeNumSelect", function (stakeNum) {


        });

        _this.app.ui.layout.bottom.append(_this.stakeNumPanel);
        _this.panels.push(_this.stakeNumPanel);

        _this.videoPlayer = new uMap.VideoPlayer({
            "title": "无人机视频"
        });
        _this.videoPlayer.addClass("UavFlightsModule");
        _this.videoPlayer.video.removeAttr("controls");
        _this.videoPlayer.video.removeAttr("autoplay");


        _this.panels.push(_this.videoPlayer);
        _this.app.ui.layout.earthOverlay.append(_this.videoPlayer);

    }

    var _class = UavFlightsModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "open"              : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {
                    _this.isOpened = true;

                    _this.layer.open();
                    _this.globeButtonPanel.open(false);
                    _this.playPanel.open(false);
                    _this.stakeNumPanel.open(false);
                    _this.videoPlayer.open(false);

                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadInit"          : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.app.modules.road.stakeNumModule.loadInit(function () {
                        _this.app.modules.road.roadLineModule.loadInit(function () {

                            _this.stakeNumPanel._startStakeNum = _this.app.modules.road.stakeNumModule.startStakeNum;
                            _this.stakeNumPanel._stopStakeNum = _this.app.modules.road.stakeNumModule.stopStakeNum;
                            _this.stakeNumPanel.update();

                            _this.request.send({}, function (json) {

                                var list = [];

                                $.each(json.data, function (i, route) {

                                    _this.routes[route.name] = route;

                                    list.push({
                                        "text" : route.title,
                                        "value": route.name
                                    });

                                    $.each(route.flights, function (j, flight) {

                                        flight.dataURL = route.baseURL + flight.file + ".json";
                                        flight.videoURL = route.baseURL + flight.file + ".mp4";
                                        flight.app = _this.app;
                                        flight.module = _this;
                                        flight.route = route;

                                        var uavFlight = new UavFlight(flight);

                                        _this.layer.dataSources.entities.add(uavFlight.path.object);
                                        _this.layer.dataSources.entities.add(uavFlight.entity.object);

                                        uavFlight.on("end", function () {
                                            _this.gotoNextFlight();
                                        });

                                        route.flights[j] = uavFlight;


                                    });

                                });

                                _this.globeButtonPanel.routeSelect.setList(list);

                                _this.globeButtonPanel.routeSelect.on("select", function (select) {

                                    if (_this.currentRoute !== _this.routes[select.value]) {

                                        if (_this.currentRoute) {
                                            $.each(_this.currentRoute.flights, function (k, flight) {
                                                flight.close();
                                            });
                                        }

                                        _this.currentRoute = _this.routes[select.value];
                                        $.each(_this.currentRoute.flights, function (k, flight) {

                                            if (k === 0) {
                                                flight.open(function () {
                                                    _this.gotoFirstFlight();
                                                });
                                            } else {
                                                flight.open();
                                            }

                                        });


                                    }

                                });

                                _this.globeButtonPanel.routeSelect.select(0);

                                _this.isLoaded = true;

                                _this.trigger("loadInit");
                                callback && callback();

                            });


                        });

                    });


                } else {
                    callback && callback();
                }

            }
        },
        "toggle"            : {
            value: function () {
                var _this = this;
                if (_this.isOpened) {
                    _this.close();
                } else {
                    _this.open();
                }
            }
        },
        "close"             : {
            value: function () {
                var _this = this;
                _this.closePanel();
                // $.each(_this.items, function (i, item) {
                //     item.close();
                // });

                $.each(_this.currentRoute.flights, function (i, flight) {
                    flight.close();
                });

                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "openPanel"         : {
            value: function () {
                var _this = this;
                $.each(_this.panels, function (i, panel) {
                    panel.open(false);
                });
            }
        },
        "closePanel"        : {
            value: function () {
                var _this = this;

                _this.closeAllPanel();
                _this.isPanelOpened = false;
                _this.trigger("closePanel");
            }
        },
        "play"              : {
            value: function () {

                var _this = this;

                if (!_this.isPlaying) {
                    _this.currentFlight.play();
                    _this.isPlaying = true;
                    _this.playPanel.playButton.addClass("pause");
                    _this.trigger("play");
                }


            }
        },
        "pause"             : {
            value: function () {

                var _this = this;

                if (_this.isPlaying && _this.currentFlight) {
                    _this.currentFlight.pause();
                    _this.isPlaying = false;
                    _this.playPanel.playButton.removeClass("pause");
                    _this.trigger("pause");
                }


            }
        },
        "togglePlay"        : {
            value: function () {
                var _this = this;
                if (_this.isPlaying) {
                    _this.pause();
                } else {
                    _this.play();
                }
            }
        },
        "gotoFlight"        : {
            value: function (index) {
                var _this = this;

                if (_this.currentRoute) {
                    if (index < _this.currentRoute.flights.length) {
                        _this.currentFlight = _this.currentRoute.flights[index];
                        _this.currentFlight.active();

                        if (index === 0) {
                            _this.playPanel.backwardButton.addClass("disable");
                        } else {
                            _this.playPanel.backwardButton.removeClass("disable");
                        }

                        if (index === _this.currentRoute.flights.length - 1) {
                            _this.playPanel.forwardButton.addClass("disable");
                        } else {
                            _this.playPanel.forwardButton.removeClass("disable");
                        }

                    }
                }

            }
        },
        "gotoFirstFlight"   : {
            value: function () {
                var _this = this;
                _this.gotoFlight(0);
            }
        },
        "gotoLastFlight"    : {
            value: function () {
                var _this = this;
                if (_this.currentRoute) {
                    _this.gotoFlight(_this.currentRoute.flights.length - 1);
                }
            }
        },
        "gotoPreviousFlight": {
            value: function () {
                var _this = this;
                if (_this.currentRoute && _this.currentFlight) {
                    var index = _this.currentRoute.flights.indexOf(_this.currentFlight);
                    if (index > 0) {
                        _this.gotoFlight(index - 1);
                    }
                }
            }
        },
        "gotoNextFlight"    : {
            value: function () {
                var _this = this;
                if (_this.currentRoute && _this.currentFlight) {

                    var index = _this.currentRoute.flights.indexOf(_this.currentFlight);
                    if (index < _this.currentFlight.route.flights.length) {
                        _this.gotoFlight(index + 1);
                    }

                }
            }
        }
    });

    return _class;

});